package org.myself.meeting.controller;

import org.myself.meeting.Utils.Constant;
import org.myself.meeting.model.*;
import org.myself.meeting.service.EmployeeService;
import org.myself.meeting.service.MeetingService;
import org.myself.meeting.service.MyMeetingService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.Map;

@Controller
public class MyMeetingController {

    @Autowired
    MyMeetingService myMeetingService;
    @Autowired
    EmployeeService employeeService;
    @Autowired
    MeetingService meetingService;

    //我的会议
    @RequestMapping("/mymeetings")
    @ResponseBody
    public Result MyMeetings(@RequestBody Base base) {
        Employee emp =employeeService.getEmpsByUsername(base.getUsername()) ;
        Map<String,Object> map = new HashMap<String, Object>();
        map.put("userful",myMeetingService.getMyMeetingsUserful(emp.getEmployeeid(), Constant.NORMAlSTATUS));
        map.put("unuserful",myMeetingService.getMyMeetingsUnUserful(emp.getEmployeeid(),Constant.NORMAlSTATUS));
        map.put("cancel",myMeetingService.getMyMeetingsALL(emp.getEmployeeid(),Constant.CANCELSTATUS));
        return new Result(200,"success",map);
    }

    //我的预定
    @RequestMapping("/mybookings")
    @ResponseBody
    public Result myBookings(@RequestBody Base base){
        Employee emp =employeeService.getEmpsByUsername(base.getUsername()) ;
        Map<String,Object> map = new HashMap<String, Object>();
        map.put("mymeeting",myMeetingService.getMyMetByme(emp.getEmployeeid(),Constant.NORMAlSTATUS));
        map.put("cancel",myMeetingService.getMyMetByme(emp.getEmployeeid(),Constant.CANCELSTATUS));
        map.put("tiral",myMeetingService.getMyMetByme(emp.getEmployeeid(),Constant.TRIALSTATUS));
        map.put("tiralcacel",myMeetingService.getMyMetByme(emp.getEmployeeid(),Constant.CANCELTRIALSTATUS));
        return new Result(200,"success",map);
    }


    //查看/撤销会议
    @RequestMapping("/mymeetingdetails")
    @ResponseBody
    public Result myMeetingDetails(@RequestBody Mymeetingdetails acc){
        Map<String,Object> map = new HashMap<String, Object>();
        map.put("meeting",meetingService.getMetByIdSTATUS(acc.getId(),Constant.NORMAlSTATUS));
        map.put("people",meetingService.getMetPerById(acc.getId()));
        return new Result(200,"success",map);
    }

    //取消会议
    @RequestMapping("/cancelmeeting")
    @ResponseBody
    public Result cancelMeeting(@RequestBody Cancelmeeting  acc){
        if (acc.getDescription().length() == 0){
            return new Result(400,"请输入撤销原因",null);
        }
        Employee emp =employeeService.getEmpsByUsername(acc.getUsername()) ;
        Integer result = meetingService.updateDesStatus(acc.getDescription(),Constant.CANCELTRIALSTATUS,acc.getId(),emp);
        if(result == 1){
            return new Result(200,"success",null);
        }
        return new Result(400,"更新失败",null);

    }

}
